package cn.edu.zzuli.wechat.login.controller;


import cn.edu.zzuli.common.result.Result;
import cn.edu.zzuli.common.result.ResultInfo;
import cn.edu.zzuli.model.entity.Identity;
import cn.edu.zzuli.model.vo.wx.WXAuth;
import cn.edu.zzuli.wechat.filter.NoAuth;
import cn.edu.zzuli.wechat.login.service.UserLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user")
@Slf4j
public class UserLoginController {

    @Autowired
    private UserLoginService userLoginServiceImpl;

    @NoAuth
    @PostMapping("/login.do")
    public ResultInfo getSessionId(@RequestBody WXAuth wxAuth, HttpSession session){
        log.info("wxAuth ==> {}",wxAuth);
        ResultInfo resultInfo = userLoginServiceImpl.getSessionId(wxAuth);
        log.info("{}",resultInfo);
        return resultInfo;
    }

    @RequestMapping("/identity.do")
    public ResultInfo identity(Identity identity, HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        identity.setIuid(uid);
        userLoginServiceImpl.checkIdentity(identity);
        return Result.ok();
    }

}
